function initialize() {

	if(dojo.byId("txtDate"))
		dojo.connect(dojo.byId("txtDate"),			"keyup",		checkKey);

	if(dojo.byId("txtValue"))
		dojo.connect(dojo.byId("txtValue"),			"keyup",		checkKey);

   if(dojo.byId("txtPayments"))
		dojo.connect(dojo.byId("txtPayments"),		"keyup",		checkKey);

	if(dojo.byId("cmbCategory"))
		dojo.connect(dojo.byId("cmbCategory"),		"keyup",		checkKey);

   if(dojo.byId("txtIncome"))
		dojo.connect(dojo.byId("txtIncome"),		"keyup",		checkKey);

	if(dojo.byId("btnSubmit"))
		dojo.connect(dojo.byId("btnSubmit"),		"click",		checkForm);

   if(dojo.byId("txtPass"))
		dojo.connect(dojo.byId("txtPass"),			"keyup",		function(){if(giveMeKey(event) == 13) logIn();});

   if(dojo.byId("txtUser")) {
		dojo.connect(dojo.byId("txtUser"),			"keyup",		function(){if(giveMeKey(event) == 13) logIn();});
		dojo.byId("txtUser").focus();
	}

   if(dojo.byId("btnLogIn"))
		dojo.connect(dojo.byId("btnLogIn"),			"click",		logIn);

	if(dojo.byId("tblHistory")) {
		dojo.connect(dojo.byId("aCollapseHistory"),	"click",		showHideHistory);
		dojo.xhrPost({handleAs: "text", load: buildHistory, url: BURL+"/scripts/ajaxMethods.php", content: {method: "buildSpentHistory"}});
	}

   if(dojo.byId("tblFullSpentHistory"))
		dojo.xhrPost({handleAs: "text", load: buildFullSpentHistory, url: BURL+"/scripts/ajaxMethods.php", content: {method: "buildFullSpentHistory"}});

	if(dojo.byId("tblPaymentsthisMonth"))
		dojo.xhrPost({handleAs: "text", load: buildMaymentsThisMonth, url: BURL+"/scripts/ajaxMethods.php", content: {method: "buildPaymentsThisMonth"}});

	return;
}

function checkKey(event) {

   var key;
	if(navigator.appCodeName == "Mozilla")
		key = event.keyCode;
	else
		key = event.which;

	if(key == 13) {
		checkForm(event);
	}
	else {
		checkContent(dojo.attr(event.target, "contentType"), event.target.value);
	}
}

function giveMeKey(event) {

   var key;
	if(navigator.appCodeName == "Mozilla")
		key = event.keyCode;
	else
		key = event.which;

	return key;
}

function checkForm(event) {

	var blnError = false;

	if(dojo.byId("txtDate")) {
		if(checkContent(dojo.attr(dojo.byId("txtDate"), "contentType"), dojo.byId("txtDate").value))
			dojo.byId("errDate").innerHTML = "&nbsp;";
		else {
			dojo.byId("errDate").innerHTML = "&nbsp;error";
			blnError = true;
		}
	}

	if(dojo.byId("txtValue")) {
	   if(checkContent(dojo.attr(dojo.byId("txtValue"), "contentType"), dojo.byId("txtValue").value)) {
			dojo.byId("errValue").innerHTML = "&nbsp;";
			var flValue = new Number(dojo.byId("txtValue").value);
			dojo.byId("txtValue").value = flValue.toFixed(2);
		}
		else {
			dojo.byId("errValue").innerHTML = "&nbsp;error";
			blnError = true;
		}
	}

	if(dojo.byId("txtPayments")) {
	   if(checkContent(dojo.attr(dojo.byId("txtPayments"), "contentType"), dojo.byId("txtPayments").value))
			dojo.byId("errPayments").innerHTML = "&nbsp;";
		else {
			dojo.byId("errPayments").innerHTML = "&nbsp;error";
			blnError = true;
		}
	}

	if(dojo.byId("txtIncome")) {
	   if(checkContent(dojo.attr(dojo.byId("txtIncome"), "contentType"), dojo.byId("txtIncome").value))
			dojo.byId("errIncome").innerHTML = "&nbsp;";
		else {
			dojo.byId("errIncome").innerHTML = "&nbsp;error";
			blnError = true;
		}
	}

	if(!blnError)
		addSpent(event);

	dojo.stopEvent(event);
}

function checkContent(strContentType, strValue) {

	switch(strContentType) {
		case "currency":
			return fnCheckCurrency(strValue);
			break;

		case "date":
			return fnCheckDate(strValue);
			break;

		case "int":
			return fncheckInteger(strValue);
			break;
	}
	return;
}

function buildHistory(strData) {

	dojo.byId("tblHistory").innerHTML = strData;
	return;
}

function buildFullSpentHistory(strData) {

	dojo.byId("tblFullSpentHistory").innerHTML = strData;
	return;
}

function buildMaymentsThisMonth(strData) {

	dojo.byId("tblPaymentsthisMonth").innerHTML = strData;
	return;
}

function showHideHistory(event) {

	if(dojo.style(dojo.byId("tblHistory"), "visibility") == "visible") {
		dojo.style(dojo.byId("tblHistory"), "visibility", "collapse");
		dojo.byId("aCollapseHistory").innerHTML = '<font style="font-size: 12px;">[+]</font>';
	}
   else if(dojo.style(dojo.byId("tblHistory"), "visibility") == "collapse") {
		dojo.style(dojo.byId("tblHistory"), "visibility", "visible");
		dojo.byId("aCollapseHistory").innerHTML = '<font style="font-size: 12px;">[-]</font>';
	}

	dojo.stopEvent(event);
	return;
}

function showFullHistory() {

	alert("showFullHistory");
	return;
}

function addSpent(event) {

	var objContent = new Object();
	objContent.method			= "insertSpent";
	objContent.date			= dojo.byId("txtDate").value;
	objContent.value			= dojo.byId("txtValue").value;
	objContent.payments		= dojo.byId("txtPayments").value;
	objContent.category		= dojo.byId("cmbCategory").value;
	objContent.description	= dojo.byId("txtDescription").value;

	dojo.xhrPost({handleAs: "text", load: buildHistory, url: BURL+"/scripts/ajaxMethods.php", content: objContent});
	clearControls();
	dojo.stopEvent(event);
	return;
}

function clearControls() {

	if(dojo.byId("txtDate")) {
		objDate = new Date;
		dojo.byId("txtDate").value = objDate.getDate() + "/" + (objDate.getMonth()+1) + "/" + objDate.getFullYear();
	}

   if(dojo.byId("txtValue"))
		dojo.byId("txtValue").value = "";

   if(dojo.byId("txtPayments"))
		dojo.byId("txtPayments").value = "1";

   if(dojo.byId("txtDescription"))
		dojo.byId("txtDescription").value = "";

	return;
}

function fnCheckDate(strDate) {

	if(strDate == "")
		return false;

   for(var i = 0; i < strDate.length; i++) {
		if(!(strDate[i] <= "9" || strDate[i] >= "0") && strDate[i] != "/")
			return false;
	}

	var arrLongMonths		= new Array(1, 3, 5, 7, 8, 10, 12);
	var arrShortMonths	= new Array(4, 6, 9, 11);

	var objDate = new Date();
	var arrDate = strDate.split("/", 3);
	if(arrDate.length != 3)
		return false;

	if(arrDate[2] > objDate.getFullYear() || arrDate[2] < objDate.getFullYear())
		return false;

	if(arrDate[1] > "12")
		return false;

	if((arrDate[0] == "31" && dojo.indexOf(arrLongMonths, arrDate[1]) == -1) || arrDate[0] > "31")
		return false;

	if(arrDate[1] == 2) {
		if(arrDate[0] > "28") {
			if(objDate.getFullYear() % 4 == 0 && arrDate[0] == 29)
				return true;
			else {
				return false;
			}
		}
	}
	return true;
}

function fnCheckCurrency(strValue) {

	if(strValue == "")
		return false;

	for(var i = 0; i < strValue.length; i++) {
		if(!(strValue[i] >= "0" && strValue[i] <= "9") && strValue[i] != "." && strValue[i] != ",")
			return false;
	}

	if(strValue == "")
		return false;

	var strSeparator;
	var intPos;

	if(strValue.indexOf(".") > -1) {
		strSeparator = ".";
		intPos = strValue.indexOf(".");
	}
	else if(strValue.indexOf(",") > -1) {
		strSeparator = ",";
		intPos = strValue.indexOf(",");
	}

	if(strSeparator == '')
		return true;

	if(strValue.indexOf(strSeparator, intPos + 1) > -1)
		return false;
	else
		return true;
}

function fncheckInteger(strValue) {

	if(strValue == "" || strValue == "0")
		return false;

	for(var i = 0; i < strValue.length; i++) {
		if(strValue[i] > "9" || strValue[i] < "0")
			return false;
	}
	return true;
}

function logIn() {

   var objContent				= new Object();
	objContent.method			= "logIn";
	objContent.user			= dojo.byId("txtUser").value;
	objContent.pass			= dojo.byId("txtPass").value;
	dojo.xhrPost({handleAs: "text", load: logInResponse, url: BURL+"/scripts/ajaxMethods.php", content: objContent});
}

function logInResponse(strResponse) {

	if(strResponse == "OK")
		document.location = BURL + "/spent.php";
	else {
		alert("<?=TXT00027?>");
		document.location = BURL + "/login.php";
	}

	return;
}

function logOff() {

   var objContent				= new Object();
	objContent.method			= "logOff";
	dojo.xhrPost({handleAs: "text", load: function(){document.location = BURL+"/login.php"}, url: BURL+"/scripts/ajaxMethods.php", content: objContent});
}